//
//  visitorView.swift
//  weibo
//
//  Created by 黄龙飞 on 15/11/22.
//  Copyright © 2015年 黄龙飞. All rights reserved.
//

import UIKit

class visitorView: UIView {
    private func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.removedOnCompletion = false
        
        // 添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }

    func setupInfo(imageName: String?, title: String) {
        
        messageLbl.text = title
        
        // 如果图片名称为 nil，说明是首页，直接返回
        guard let imgName = imageName else {
            // 播放动画
            startAnim()
            
            return
        }
        
        iconView.image = UIImage(named: imgName)
        // 隐藏小房子
        homeView.hidden = true
        // 将遮罩图像移动到底层
        sendSubviewToBack(maskIconView)
    }

    override init(frame:CGRect){
    super.init(frame: frame)
        //要写
        setupUI()
    }
    required init?(coder aDecoder:NSCoder){
//    fatalError("init(coder:) has not been implemented")
        super.init(coder:aDecoder)
        setupUI()
    }
    private lazy var iconView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var homeView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var maskIconView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var messageLbl:UILabel=UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")
    
}

extension visitorView{
    private func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(messageLbl)
        addSubview(registerButton)
        addSubview(loginButton)
        for v in subviews{
        v.translatesAutoresizingMaskIntoConstraints=false
        }
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLbl, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLbl, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLbl, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLbl, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "regButton": registerButton]))
        
        // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
}
